﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class TestVfxValid : MonoBehaviour {
    static string project_path = Application.dataPath.Substring(0, Application.dataPath.Length - 6);
    static int go_count = 0, components_count = 0, missing_count = 0;

    // Use this for initialization
    void Start () {
	    
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    //[]

    //string[] GetSelectPaths()
    //{
    //    UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered);
    //    //此处添加需要命名的资源后缀名,注意大小写。
    //    string[] paths = new string[SelectedAsset.Length];

    //    for (int i = 0; i < paths.Length; i++)
    //    {
    //        paths[i] = project_path + AssetDatabase.GetAssetPath(SelectedAsset[i]);
    //    }

    //    return paths;
    //}

    public void OnGUI()
    {
        if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
        {
            FindInSelected();
        }
        if (GUILayout.Button("Find Missing Mat in selected Particle"))
        {
            FindInSelectedMissMat();
        }
    }

    private static string GetRootStr(GameObject g)
    {
        string s = g.name;
        Transform t = g.transform;
        while (t.parent != null)
        {
            s = t.parent.name;
            t = t.parent;
        }

        return s;
    }

    private static string GetPathStr(GameObject g)
    {
        string s = g.name;
        Transform t = g.transform;
        while (t.parent != null)
        {
            s = t.parent.name + "/" + s;
            t = t.parent;
        }

        return s;
    }

    #region 检查丢材质的特效节点
    private static void FindInSelectedMissMat()
    {
        GameObject[] Go = Selection.gameObjects;
        go_count = 0;
        components_count = 0;
        missing_count = 0;

        //FileStream fs = new FileStream("log/MissMaterialLog.txt", FileMode.OpenOrCreate);
        string path = project_path + "/log/";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        StreamWriter swriter = new StreamWriter(path + "MissMaterialLog.txt",false);

        foreach (GameObject g in Go)
        {
            FindMissMat(g, swriter);
        }

        string str = string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count);
        Debug.Log(str);
        //swriter.WriteLine(str);

        swriter.Close();
        swriter = null;
        //fs.Close();
        //fs = null;
    }

    private static void FindMissMat(GameObject go,StreamWriter swriter)
    {
        go_count++;
        ParticleSystem[] pars = go.GetComponentsInChildren<ParticleSystem>();
        for (int i = 0; i < pars.Length; i++)
        {
            components_count++;
            ParticleSystemRenderer rend = pars[i].GetComponent<ParticleSystemRenderer>();
            if (rend != null && rend.material.mainTexture == null)
            {
                missing_count++;
                if(rend.gameObject.name == "play")
                {
                    continue;
                }
                string s = GetPathStr(rend.gameObject);
                //Object.DestroyImmediate(components[i]);
                //s = s + " has an empty script attached in position: " + i;
                Debug.Log(s, rend);
                swriter.WriteLine(s);
            }
        }

		TrailRenderer[] pars2 = go.GetComponentsInChildren<TrailRenderer>();
		for (int i = 0; i < pars2.Length; i++)
		{
			components_count++;
			TrailRenderer rend = pars2[i];
			if (rend != null && rend.material.mainTexture == null)
			{
				missing_count++;
				if(rend.gameObject.name == "play")
				{
					continue;
				}
				string s = GetPathStr(rend.gameObject);
				//Object.DestroyImmediate(components[i]);
				//s = s + " has an empty script attached in position: " + i;
				Debug.Log(s, rend);
				swriter.WriteLine(s);
			}
		}
    }
    #endregion

    #region 检查丢脚本组件的节点
    private static void FindInSelected()
    {
        GameObject[] Go = Selection.gameObjects;
        go_count = 0;
        components_count = 0;
        missing_count = 0;
        foreach (GameObject g in Go)
        {
            FindInGO(g);
        }
        Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
    }

    private static void FindInGO(GameObject g)
    {
        go_count++;
        Component[] components = g.GetComponents<Component>();
        for (int i = 0; i < components.Length; i++)
        {
            components_count++;
            if (components[i] == null)
            {
                missing_count++;
                string s = GetPathStr(g);
                //Object.DestroyImmediate(components[i]);
                Debug.Log(s + " has an empty script attached in position: " + i, g);
            }
        }
        // Now recurse through each child GO (if there are any):
        foreach (Transform childT in g.transform)
        {
            //Debug.Log("Searching " + childT.name  + " " );
            FindInGO(childT.gameObject);
        }
    }
    #endregion

}
